package persistence.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import persistence.db.DataSet;
import persistence.db.DatabaseConnector;
import persistence.model.AbstractModel;
import persistence.model.AnneeEtude;
import persistence.model.Conge;
import persistence.model.Semestre;

public class CongeDAO extends AbstractDAO{
	
	private Conge conge;
	
	/**
	 * Constructeur d'un conge DAO
	 */
	public CongeDAO() {
		setTableName("CONGE");
	}
	
	/**
	 * Constructeur d'un conge DAO
	 * @param c conge correspondant
	 */
	public CongeDAO(Conge c) {
		this();
		conge = c;
		setId(c.getId());
	}

	public DataSet all() {
		return new DataSet(this);
	}

	public AbstractModel makeModel(ResultSet rset) {
		try {
			setId(rset.getInt("id_conge"));
			System.out.println("id Conge :" + this.getId());
			return new Conge(this.getId(), 
					dateToString(rset.getDate("dateDebutConge")), 
					dateToString(rset.getDate("dateFinConge")), 
					rset.getInt("FK_CONGE_AE"));
		} catch (SQLException e) {
			System.out.println("makeModel error");
			e.printStackTrace();
		}
		return null;
	}

	public void save() {
		String query;
		ResultSet result;
			query = "INSERT INTO " + getTableName() + " VALUES ( S_PK_"+ getTableName() +".nextval, '" +
			conge.getDateDebutConge() + "', '" +
			conge.getDateFinConge() +"', "+
			conge.getIdAnneeEtude() +")";
			/* Execute la requete */
			result = DatabaseConnector.getInstance().execute(query);
			setId(this.getId()); /* Mise a jour de l'identifiant */
	}

	public void save(int id) {
		String query;
		ResultSet result;
			query = "UPDATE " + getTableName() +
			" SET dateDebutConge = '" + conge.getDateDebutConge() +
			"', dateFinConge = '" + conge.getDateFinConge() +
			"', fk_conge_AE = "+ conge.getIdAnneeEtude() +
			" WHERE ID_"+ getTableName() + " = "+ id;
			/* Execute la requete */
			result = DatabaseConnector.getInstance().execute(query);
	}

}
